package com.ruoyi.monitor.utils.PythonServer;

import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class PyConnect {

    private HttpURLConnection con;

    /**
     *
     * @param u 请求路径
     * @param jsonObject json数据格式
     * @param method 请求方法，GET OR POST
     * @return
     */

    public HttpURLConnection getConnection(String u, JSONObject jsonObject,String method){
        HttpURLConnection con = null;
        try {
            URL url = new URL(u);
            //URL url = new URL("http://localhost:5000/train");
            con  = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            con.setRequestProperty("Content-Length", String.valueOf(jsonObject.toString().getBytes().length));
            con.setRequestMethod(method);
            con.setDoOutput(true);
            // 设置是否从httpcon读入，默认情况下是true;
            con.setDoInput(true);
            // Post 请求不能使用缓存
            con.setUseCaches(false);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.con = con;
        return con;
    }


    public void write(JSONObject jsonObject){
        OutputStream out = null;
        try {
            out = con.getOutputStream();
            out.write(jsonObject.toString().getBytes());
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 小数向上取整
     * @return
     */
    public List<Integer> getResult(){
        List<Integer> res = null;
        try {
            InputStream inputStream = con.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            String c = content.toString();
            System.out.println(c);
            if (c.contains("[")&&c.contains("]")){
                String replaceAll = c.replaceAll("\\[", "").replaceAll("\\]", "");
                String[] split = replaceAll.split(" ");
//            System.out.println(split.toString());
                res = new ArrayList<>();
                try {
                    for (int i = 0; i < split.length; i++) {

                        double num = Double.parseDouble(split[i]);
                        res.add(Math.toIntExact(Math.round(num)));
//                        System.out.println(res.get(i));
                    }
                }catch (Exception e){
                    System.out.println("数据非数组");
                    return null;
                }

            }
        } catch (IOException e) {
            System.out.println("请检查Python服务器是否启动");
            e.printStackTrace();
        }
     return res;
    }

    public List<Double> getResultD(){
        List<Double> res = null;
        try {
            InputStream inputStream = con.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            String c = content.toString();
            System.out.println(c);
            if (c.contains("[")&&c.contains("]")){
                String replaceAll = c.replaceAll("\\[", "").replaceAll("\\]", "");
                String[] split = replaceAll.split(" ");
//            System.out.println(split.toString());
                res = new ArrayList<>();
                try {
                    for (int i = 0; i < split.length; i++) {
                        double num = Double.parseDouble(split[i]);
                        res.add(num);
//                        System.out.println(res.get(i));
                    }
                }catch (Exception e){
                    System.out.println("数据非数组");
                    return null;
                }

            }
        } catch (IOException e) {
            System.out.println("请检查Python服务器是否启动");
            e.printStackTrace();
        }
        return res;
    }


}
